package com.atguigu.book.controller;

import com.atguigu.book.pojo.Book;
import com.atguigu.book.service.BookService;
import com.atguigu.myssm.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName: BookContorller
 * @Description:
 * @Author: wty
 * @Date: 2022/12/12
 */

public class BookContorller {
    BookService bookService;

    public String index(Integer minPrice, Integer maxPrice, Integer pageNo, HttpSession session) {
        if (null == pageNo) {
            pageNo = 1;
        }
        Object minPriceObj = session.getAttribute("minPrice");
        if (null != minPriceObj) {
            minPrice = (Integer) minPriceObj;
        }

        Object maxPriceObj = session.getAttribute("maxPrice");
        if (null != maxPriceObj) {
            maxPrice = (Integer) maxPriceObj;
        }

        List<Book> bookList = bookService.getBookList(minPrice, maxPrice, pageNo);
        session.setAttribute("bookList", bookList);
        return "index";
    }

    public String pageByPrice(ServletRequest request, ServletResponse response, HttpSession session) throws ServletException, IOException {
        Integer minPrice = null;
        Integer maxPrice = null;
        String minPriceStr = request.getParameter("minPrice");
        if (StringUtil.isNotEmpty(minPriceStr)) {
            minPrice = Integer.valueOf(minPriceStr);
        }

        String maxPriceStr = request.getParameter("maxPrice");
        if (StringUtil.isNotEmpty(maxPriceStr)) {
            maxPrice = Integer.valueOf(maxPriceStr);
        }

        session.setAttribute("minPrice", minPrice);
        session.setAttribute("maxPrice", maxPrice);

        // 会跳转index方法
        return "redirect:book.do";
    }
}
